package com.szy.redstonemcu;

import net.minecraft.block.Block;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.relauncher.Side;
import org.apache.logging.log4j.Logger;
@Mod.EventBusSubscriber(modid = "redstonemcu")
@Mod(modid = RedstoneMCU.MODID, name = RedstoneMCU.NAME, version = RedstoneMCU.VERSION)
public class RedstoneMCU
{
    public static final String MODID = "redstonemcu";
    public static final String NAME = "Redstone MCU";
    public static final String VERSION = "1.0";

    public static SimpleNetworkWrapper simpleNetworkWrapper;

    @Mod.Instance(RedstoneMCU.MODID)
    public static RedstoneMCU instance;
    public static Logger logger;

    @EventHandler
    public void preInit(FMLPreInitializationEvent event)
    {
        logger = event.getModLog();
    }

    @EventHandler
    public void init(FMLInitializationEvent event)
    {
        // some example code
        //logger.info("DIRT BLOCK >> {}", Blocks.DIRT.getRegistryName());
        simpleNetworkWrapper = NetworkRegistry.INSTANCE.newSimpleChannel("redstonemcu");
        //simpleNetworkWrapper.registerMessage(MessageHandlerOnServer.class, MCUMessage.class, 1, Side.SERVER);
        if(event.getSide() == Side.CLIENT){
            //ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(RedstoneMCU.rm8faa), 0, new ModelResourceLocation(RedstoneMCU.rm8faa.getRegistryName(), "inventory"));
            RedstoneMCU.simpleNetworkWrapper.registerMessage(MessageHandlerOnClient.class, McuMessage.class, 2, Side.CLIENT);
            logger.info("register client wrapper");
        }//else if(event.getSide() == Side.SERVER){
            simpleNetworkWrapper.registerMessage(MessageHandlerOnServer.class, McuMessage.class, 1, Side.SERVER);
            logger.info("register server wrapper");
        //}
    }

    public static Block mcuBase;
    public static Item mcuBaseItem,flashItem;

    @SubscribeEvent
    public static void registerBlock(RegistryEvent.Register<Block> event){
        event.getRegistry().register(mcuBase =new Rm32s1Block().setCreativeTab(RM_CREATIVE_TAB).setUnlocalizedName("redstonemcu.rm32s1").setRegistryName("redstonemcu","rm32s1"));
        GameRegistry.registerTileEntity(Rm32s1Entity.class,new ResourceLocation("redstonemcu","rm32s1"));
        //NetworkRegistry.INSTANCE.registerGuiHandler(instance, GuiHandlerRegistry.getInstance());
        //GuiHandlerRegistry.getInstance().registerGuiHandler(new GuiProxy(),1);
        //ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(rm8faa), 0, new ModelResourceLocation(rm8faa.getRegistryName(), "inventory"));
    }

    @SubscribeEvent
    public static void registerItem(RegistryEvent.Register<Item> event){
        event.getRegistry().register(mcuBaseItem =new ItemBlock(mcuBase).setRegistryName(mcuBase.getRegistryName()).setMaxStackSize(1));
        event.getRegistry().register(flashItem=new Flash().setRegistryName(new ResourceLocation(MODID,"flash")));

    }

//    @SubscribeEvent
//    public static void onModelReg(ModelRegistryEvent event) {
//        ModelLoader.setCustomModelResourceLocation(rm8faa_item, 0, new ModelResourceLocation(rm8faa_item.getRegistryName(), "inventory"));
//    }

//    @SubscribeEvent
//    public static void onModelRegistration(ModelRegistryEvent event){
//        ModelLoader.setCustomModelResourceLocation(rm8faa.getIte, 0, new ModelResourceLocation(new ResourceLocation("my_mod", "example_item_model"), "inventory"));
//    }
    public static final CreativeTabs RM_CREATIVE_TAB = new CreativeTabs("redstonemcu") {
        @Override
        public ItemStack getTabIconItem() {
            return new ItemStack(Items.DIAMOND);
        }


    };
}


